package com.leyou.item.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.leyou.common.exception.pojo.ExceptionEnum;
import com.leyou.common.exception.pojo.LyException;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 */
@Service
@Transactional
public class CateogroyService {

    @Autowired
    private CategoryMapper categoryMapper;

    public List<Category> findCategoriesByPid(Long pid) {
        //1.封装条件
        Category category = new Category();
        category.setParentId(pid);

        QueryWrapper<Category> queryWrapper = Wrappers.query(category);

        //2.执行查询，获取结果
        List<Category> categories = categoryMapper.selectList(queryWrapper);

        //3.处理并返回结果
        if(CollectionUtils.isEmpty(categories)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return categories;
    }

    public List<Category> findCategoriesByCids(List<Long> ids) {
        //selectBatchIds: 根据id批量查询
        List<Category> categories = categoryMapper.selectBatchIds(ids);
        if(CollectionUtils.isEmpty(categories)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return categories;
    }
}
